package com.superme.ResConsumerServiceByribbon.configure;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.*;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;

/**
 * Created by Administrator on 2017/6/23.
 */
@Configuration
public class BizserverConfiguration {
    /*
    @Bean
    public OAuth2RestTemplate getOAuth2RestTemplate(OAuth2ClientContext oauth2Context) {

        OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource(),oauth2Context);

        return restTemplate;
    }*/
    @Autowired
    OAuth2ClientContext oauth2Context;



    @Bean
    @ConfigurationProperties(prefix = "security.oauth2.client") /*如果配置文件中没有配security.oauth2.client也不影响*/
    public ClientCredentialsResourceDetails clientCredentialsResourceDetails() {
        ClientCredentialsResourceDetails ccr =  new ClientCredentialsResourceDetails();

        return ccr;
    }

    @LoadBalanced
    @Bean
    public OAuth2RestTemplate clientCredentialsRestTemplate() {
        return new OAuth2RestTemplate(clientCredentialsResourceDetails(),oauth2Context);
    }

    //配置RIBBON 的策略
    @Bean
    public IRule ribbonRule()
    {
        //RandomRule() RoundRobinRule() RetryRule()
        //return new RoundRobinRule();
        return new RoundRobinRule();

    }


    /*
    @Bean
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }
    */

}
